package cn.edu.ldu.protocol;

import com.alibaba.fastjson.JSON;
import cn.edu.ldu.modules.controller.ChatHistoryController;
import cn.edu.ldu.views.modules.controller.UserController;
import cn.edu.ldu.modules.entity.ChatHistoryEntity;
import cn.edu.ldu.util.Translate;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.List;

public class GetChatHistory {

    public GetChatHistory(Protocol protocol) throws IOException {
        ChatHistoryEntity che;
//        che =  (ChatHistoryEntity)Translate.ByteToObject(cn.edu.ldu.protocol.getData());
        che = JSON.parseObject(protocol.getData(), ChatHistoryEntity.class);

        List<ChatHistoryEntity> chatHistories = null;
        try {
            chatHistories = ChatHistoryController.getHistoryByUserId(che.getSenderId(), che.getTalkerId(), protocol.getToken());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(chatHistories != null){
            protocol.setKey("ChatHistory");
            protocol.setData(JSONObject.toJSONString(chatHistories));
            UserController.sendDataToUser(protocol);
        }
    }

}
